/**
 * Copyright 2011 Marco Berri - marcoberri@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */
package com.mbfasttrack.controller;

import com.mbfasttrack.model.Tag;
import com.mbfasttrack.service.TagService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author Marco Berri marcoberri@gmail.com http://tecnicume.blogspot.com
 */
@Controller
public class TagController extends Base {

    /**
     *
     * @return
     */
    @ModelAttribute("tag")
    public Tag getTagObject() {
        return new Tag();
    }

    /**
     *
     * @param tagService
     */
    @Autowired(required = true)
    public void setTagService(TagService tagService) {
        this.tagService = tagService;

    }

    /**
     *
     * @param tag
     * @param model
     * @return
     * @throws MessagingException
     */
    @RequestMapping(value = "/tag/save", method = RequestMethod.POST)
    public String save(@ModelAttribute("tag") Tag tag, Model model) throws MessagingException {

        Tag t = tagService.add(tag.getName(), -1);
        model.addAttribute("output", t);
        return "tag/saveresult";
    }

    /**
     *
     * @param like
     * @param response
     * @throws JsonGenerationException
     * @throws JsonMappingException
     * @throws IOException
     */
    @RequestMapping(value = "/tag/json/search", method = RequestMethod.GET)
    public void jsonSearch(@RequestParam("term") String like, HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException {

        List<Tag> list = tagService.findAllByLikeName(like);

        ArrayList result = new ArrayList();
        for (Tag t : list) {
            result.add(t.getName());
        }

        ObjectMapper mapper = new ObjectMapper();


        String jsonString = mapper.writeValueAsString(result);

        AbstractHttpMessageConverter<String> stringHttpMessageConverter = new StringHttpMessageConverter();

        MediaType jsonMimeType = MediaType.APPLICATION_JSON;

        if (stringHttpMessageConverter.canWrite(String.class, jsonMimeType)) {

            try {

                stringHttpMessageConverter.write(jsonString, jsonMimeType, new ServletServerHttpResponse(response));

            } catch (Exception e) {
                log.fatal(e);
            }
        }

        //return null;
    }
}